﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Text;
using System.Windows;
using eLearning.Model;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;

namespace eLearning.View
{
    public partial class WikiCategoryFormPage : PhoneApplicationPage
    {
        private Category kategori;
        public ObservableCollection<Category> kategoris;
        private object data = null;
        private bool firstLoaded;

        public WikiCategoryFormPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(RssItemPage_Loaded);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
                firstLoaded = false;
            else
                firstLoaded = true;
        }

        #region Event Handlers
        private void RssItemPage_Loaded(object sender, RoutedEventArgs e)
        {
            data = DataContext;
            if (firstLoaded)
            {
                if (data != null)
                {
                    kategori = (Category)data;
                    textBoxKategori.Text = kategori.Kategori;
                }
            }
        }

        #endregion

        private void CreateCategory(Category data)
        {
            string json = null, url = null;

            Category kat = (Category)data;
            if (kat.Id == null)
                url = Config.Instance.CATEGORY_SERVICE_URL() + Config.ADD_ACTION_KEY; //create                
            else
                url = Config.Instance.CATEGORY_SERVICE_URL() + Config.UPDATE_ACTION_KEY; //update

            json = JsonConvert.SerializeObject(kat);


            Uri uri = new Uri(url, UriKind.Absolute);
            WebClient client = new WebClient();

            client.Encoding = Encoding.UTF8;
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            client.Headers[HttpRequestHeader.ContentLength] = json.Length.ToString();

            client.UploadStringCompleted += new UploadStringCompletedEventHandler(saveCategory_UploadStringCompleted);

            try
            {
                client.UploadStringAsync(uri, "POST", json);
            }
            catch (Exception) { }
        }

        private void saveCategory_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled || e.Error != null)
                    return;

                MessageBox.Show("Message : " + e.Result);

                if (e.Result.Contains("Success"))
                    NavigationService.GoBack();

            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR : " + ex.Message);
            }
        }

        private void buttonSave_Click(object sender, System.EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxKategori.Text))
            {
                MessageBox.Show("Nama Kategori harus di isi!");
                return;
            }
            
            if (kategori == null)
                kategori = new Category();

            kategori.Kategori = textBoxKategori.Text;
            CreateCategory(kategori);
        }

        private void buttonCancel_Click(object sender, System.EventArgs e)
        {
            NavigationService.GoBack();
        }

    }
}